package cn.school.ctrl;

import cn.school.entity.Admin;
import cn.school.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {
    @Autowired
    private AdminService adminService;

    //避免和视图解析器的冲突
    @PostMapping("login")
    public String login(@RequestParam(value = "userName",required = true,defaultValue="") String name,@RequestParam("userPass") String pass, HttpServletRequest req) {
        Admin admin = adminService.login(name, pass);
        if(admin!=null){
            req.getSession().setAttribute("USER_LOGIN",admin);
            //return "welcome.jsp";
            return "redirect:/java68.html";
        }
        req.setAttribute("msg","error usernme or invalid password");
        return "forward:/userLogin.jsp";
    }
}
